#ifndef MOUSETRACKINGWORKER_H
#define MOUSETRACKINGWORKER_H

#include <QObject>
#include <QFile>
#include <QSocketNotifier>
#include <QThread>
#include <QMutex>

#include <linux/input.h>

class MouseTrackingWorker : public QObject
{
    Q_OBJECT

public:
    explicit MouseTrackingWorker(QObject *parent = nullptr);
    ~MouseTrackingWorker();

    // 设置读取函数
    double getMoveDistance();

public slots:
    void init(const QString &devicePath);
    void process();  // 读取数据并处理的槽函数

signals:
    void mouseMoved(double distance);  // 鼠标移动的信号
    void deviceDisconnected();
private:
    QFile deviceFile;
    QSocketNotifier *notifier;
    QString devicePath;
    int deviceFd= -1;
    int lastX, lastY;
    int currentX, currentY;
    double totalDistance;  // 累计的鼠标移动距离
    double xAccumulator, yAccumulator;  // 用于累加X和Y坐标的变化

    QMutex mutex;  // 用于同步访问
    void processMouseData(const input_event &event);
};

#endif // MOUSETRACKINGWORKER_H
